package com.wzu.controller;

import com.wzu.entity.Insured;
import com.wzu.entity.Result;
import com.wzu.service.InsuredService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/insured")
@CrossOrigin
public class InsuredController {
    @Autowired
    private InsuredService insuredService;

    // 查询/模糊搜索
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) String keyword) {
        List<Insured> list = insuredService.searchInsured(keyword);
        return new Result("查询成功", list, 200);
    }

    // 添加
    @PostMapping("/add")
    public Result add(@RequestBody Insured insured) {
        if (insuredService.addInsured(insured)) {
            return new Result("添加成功", null, 200);
        } else {
            return new Result("添加失败", null, 400);
        }
    }

    // 更新
    @PutMapping("/update")
    public Result update(@RequestBody Insured insured) {
        if (insuredService.updateInsured(insured)) {
            return new Result("更新成功", null, 200);
        } else {
            return new Result("更新失败", null, 400);
        }
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        if (insuredService.deleteInsured(id)) {
            return new Result("删除成功", null, 200);
        } else {
            return new Result("删除失败", null, 400);
        }
    }
} 